var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [
      {value: 'USA', name: '美国'},
      {value: 'CHN', name: '中国', checked: 'true'},
      {value: 'BRA', name: '巴西'},
      {value: 'JPN', name: '日本'},
      {value: 'ENG', name: '英国'},
      {value: 'FRA', name: '法国'},
    ],
    roles:[],
    cur_role:0,


    orderList: [],
    page: 1,
    size: 1000,
    loadmoreText: '正在加载更多数据',
    nomoreText: '全部加载完成',
    nomore: false,
    totalPages: 1,
    is_scribe:false,

    ck_val:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo=wx.getStorageSync('userInfo');
    if(userInfo!="")
    {
      this.setData({userInfo:userInfo});
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () 
  {
      var userInfo=this.data.userInfo;
      if(userInfo!="")
      {
         
        this.get_roles();
         
         
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  radioChange: function (e) 
  {
    var id=e.currentTarget.dataset.id;
    var roles=this.data.roles;
    roles.map(function(item){
      item.checked=false;
    });

    roles[id].checked=true;

    var cur_role=roles[id].ID;
    this.setData({cur_role:cur_role});

    this.setData({roles:roles});
    console.log(id)
  },
  get_roles:function()
  {
    var that=this;
    util.request(api.GetUserRoleList,{ST:1},'GET').then(function (res) {
      if (res.Status == 100) 
      {
        that.setData({roles:res.Data});
      }
    });
  },
  call_me:function(){
    wx.makePhoneCall({phoneNumber: '15107174170'})
  },
  checkboxChange:function(e){
    
    if(e.detail.value.length>0)
    {
      //console.log('checkbox发生change事件，携带value值为：', e.detail.value);//cXwNPR9BH1miAeJz3hBYL06zSE19i4SSq0u_eZ195N8
      wx.requestSubscribeMessage({
        tmplIds: ['cXwNPR9BH1miAeJz3hBYL06zSE19i4SSq0u_eZ195N8','Mro6voIQn6DT6YRbMJRZLJZJyPyzzs2hSvcYGhuKVyg','Hz6CnqL0zI_frxezYEGAh13YG3YBtUXKSjrrC6aqUps'],
        success (res) 
        {
           console.log(res);
        }
      })
    }
  },
  post:function(e)
  {
    var that=this;
    var days=e.currentTarget.dataset.days;
    var fee=e.currentTarget.dataset.fee;
    var roleid=e.currentTarget.dataset.roleid;
    var title=e.currentTarget.dataset.title;
 
    
    var userInfo=wx.getStorageSync('userInfo');
    if(userInfo=="")
    {
      wx.showModal({
        title: '提示',
        content:'请先登录',
        success(res) 
        {
          wx.reLaunch({ url: '/pages/auth/mobile/mobile' });
        }
      })
      return;
    }

    //新预约单通知【客户号码、温馨提示】  cXwNPR9BH1miAeJz3hBYL06zSE19i4SSq0u_eZ195N8
    //订单编号、订单金额、付款时间、订单备注 Mro6voIQn6DT6YRbMJRZLJZJyPyzzs2hSvcYGhuKVyg
    //订单编号、商品名称  Hz6CnqL0zI_frxezYEGAh13YG3YBtUXKSjrrC6aqUps

    var is_scribe=that.data.is_scribe;
    if(is_scribe==false)
    {
      wx.requestSubscribeMessage({
        tmplIds: ['cXwNPR9BH1miAeJz3hBYL06zSE19i4SSq0u_eZ195N8','Mro6voIQn6DT6YRbMJRZLJZJyPyzzs2hSvcYGhuKVyg','Hz6CnqL0zI_frxezYEGAh13YG3YBtUXKSjrrC6aqUps'],
        success (res) 
        {
           that.setData({is_scribe:true});
           console.log(res);
           
        }
      })
      return;
    }
    

    var token=wx.getStorageSync('wx');
    var pd={OpenId:token.openid,RoleID:roleid,UserID:userInfo.Id,Days:days,Fee:fee,Title:title};
    util.request(api.GetRolePrepayid,pd,'POST').then((res) => 
    {
      if (res.success==true) 
      {
        const payParam = res;
        app.globalData.prepay_id = res.prepay_id;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res1) 
          {
            userInfo.RoleID=roleid;
            wx.setStorageSync('userInfo',userInfo);
            wx.showModal({
              title: '提示',
              content:'充值成功',
              success(res) 
              {
                wx.reLaunch({ url: '/pages/auth/register/register' });
              }
            })
          },
          'fail': function (res2) 
          {
            wx.showModal({title:"提示",content:"充值失败"});
          },
          'complete': function (res3) 
          {
             
          }
        });
      } 
      else 
      {
         wx.showModal({title:"提示",content:"充值失败"});
         return;
      }
    });
    
    
  }
})